package com.jmu.aslongastoday.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmu.aslongastoday.common.Result;
import com.jmu.aslongastoday.domain.TbUser;
import com.jmu.aslongastoday.dto.UserLoginDto;
import com.jmu.aslongastoday.dto.UserRigsterDto;
import com.jmu.aslongastoday.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userLoginDto.getUserName());
        TbUser user = userService.getOne(queryWrapper);
        return user.getUserPass().equals(userLoginDto.getUserPassword())?Result.success("登录成功",user):Result.fail("登录失败");
    }

    @PostMapping("register")
    public Result register(@RequestBody UserRigsterDto dto){
        TbUser tbUser = new TbUser();
        tbUser.setUserName(dto.getUserName());
        tbUser.setUserPass(dto.getUserPassword());
        tbUser.setCreateTime(new Date());
        tbUser.setUpdateTime(new Date());
        tbUser.setStatus("1");
        tbUser.setUserPhone(dto.getUserPhone());

        userService.save(tbUser);

        return Result.success("");
    }
}
